/**
 * 
 */
package com.manolo.client.util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.manolo.client.entity.SUIT;

/**
 * @author gustavo
 * 
 */
public final class Util {

	public static String removeIdentification(String valor) {
		return valor.replaceAll("\\w+:", "");
	}

	public static Double parseStringToDouble(String value) {
		Double val;
		try {
			val = new Double(value.trim());
		} catch (Exception e) {
			val = new Double(0);
		}
		return val;
	}

	public static boolean parseStringToBoolean(String value) {
		Boolean val;
		try {
			val = new Boolean(value.trim());
		} catch (Exception e) {
			val = new Boolean(false);
		}
		return val;
	}

	public static Integer parseStringToInteger(String value) {
		Integer val;
		try {
			val = new Integer(value.trim());
		} catch (Exception e) {
			val = new Integer(0);
		}
		return val;
	}

	public static SUIT getSuit(int idSuit) {

		SUIT id;
		switch (idSuit) {
		case 1:
			id = SUIT.CLUBS;
			break;
		case 2:
			id = SUIT.HEARTS;
			break;
		case 3:
			id = SUIT.SPADES;
			break;
		case 4:
			id = SUIT.DIAMONDS;
			break;
		default:
			id = SUIT.NONE;
			break;
		}
		return id;
	}

	
	/**
	 * The code below works with the treatment of images
	 * Originally was done in a class named "UtilImage.java", to better organization, this was moved to here
	 */

	public static BufferedImage loadImage(String path) throws IOException {
		return ImageIO.read(new File(path));
	}

	public static void operationBlit(BufferedImage originalImage,
			Graphics2D g2d, int destinationX, int destinationY,
			int columnNumber, int area, int width, int height) {
		int coordX = (area % columnNumber) * width;
		int coordY = (area % columnNumber) * height;
		g2d.drawImage(originalImage, destinationX, destinationY, destinationX
				+ width, destinationY + height, coordX, coordY, coordX + width,
				coordY + height, null);

	}
}
